package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Model;

/**
 * Created by quxingyu on 2015/8/6.
 */
@SuppressWarnings("serial")
public class Test extends Model<Test> {
    public static final Test dao = new Test();

    /**
     * 查看测试是否存在
     * @param id
     * @param type
     * @return
     */
    public Test getTest(Long id,String type){
        return dao.findFirst("select * from t_test where target_id = ? and target_type = ?",id,type);
    }

    /**
     * 保存班级考试
     * 试卷信息
     * @param clazzLearningSetting
     * @param test
     */
    public void saveClassTest(ClazzLearningSetting clazzLearningSetting,Test test){
        Clazz clazz = Clazz.dao.findById(clazzLearningSetting.getLong("class_id"));
        test.set("title",clazz.get("name"));
        test.set("pass_proportion",SysProperties.dao.getSysPropertiesByName("clazz_test_pass_score"));
        test.set("limited_time",SysProperties.dao.getSysPropertiesByName("clazz_test_limited_time"));
        test.set("score",SysProperties.dao.getSysPropertiesByName("clazz_test_total_score"));
        test.set("item_count",SysProperties.dao.getSysPropertiesByName("clazz_test_item_count"));
        test.set("is_open", "1");
        test.set("missscore","0");
        QuestionCategory questionCategory = QuestionCategory.dao.getQuestionCategory(clazz.getLong("id"),"clazz");
        if(questionCategory==null){
            test.set("rand_categery_id","1,2");
        }else{
            test.set("rand_categery_id",String.valueOf(questionCategory.getLong("id")));
        }
        test.set("target_id",clazz.get("id"));
        test.set("target_type","faceclazztest");
        test.set("paper_make_pattern","3");
        test.set("paper_answer_pattern","2");
        test.set("rand_single_num",SysProperties.dao.getSysPropertiesByName("clazz_test_single_choice_num"));
        test.set("rand_multi_num",SysProperties.dao.getSysPropertiesByName("clazz_test_multi_choice_num"));
        test.set("rand_choose_num",SysProperties.dao.getSysPropertiesByName("clazz_test_determine_choose_num"));
        test.set("rand_fill_num",SysProperties.dao.getSysPropertiesByName("clazz_test_fill_blank_num"));
        test.set("rand_single_score",SysProperties.dao.getSysPropertiesByName("clazz_test_single_choice_score"));
        test.set("rand_multi_score", SysProperties.dao.getSysPropertiesByName("clazz_test_multi_choice_score"));
        test.set("rand_choose_score",SysProperties.dao.getSysPropertiesByName("clazz_test_determine_choose_score"));
        test.set("rand_fill_score",SysProperties.dao.getSysPropertiesByName("clazz_test_fill_blank_score"));
        test.remove("id").save();
        clazzLearningSetting.set("test_id",test.get("id"));
        clazzLearningSetting.update();
    }

    /**
     * 保存课程练习
     * @param courseLearningSetting
     * @param test
     */
    public void saveTest(CourseLearningSetting courseLearningSetting,Test test) {
        Long courseId = courseLearningSetting.getLong("course_id");
        CourseBase courseBase = CourseBase.dao.findById(courseId);
        test.set("title",courseBase.get("name"));
        test.set("pass_proportion",SysProperties.dao.getSysPropertiesByName("course_exam_pass_score"));
        test.set("limited_time",SysProperties.dao.getSysPropertiesByName("course_exam_limited_time"));
        test.set("score",SysProperties.dao.getSysPropertiesByName("course_exam_total_score"));
        test.set("item_count",SysProperties.dao.getSysPropertiesByName("course_exam_total_score"));
        test.set("times",SysProperties.dao.getSysPropertiesByName("course_exam_times"));
        test.set("is_open","1");
        test.set("missscore","0");
        QuestionCategory questionCategory = QuestionCategory.dao.getQuestionCategory(courseBase.getLong("id"),"course");
        if(questionCategory==null){
            test.set("rand_categery_id","1,2");
        }else{
            test.set("rand_categery_id",String.valueOf(questionCategory.getLong("id")));
        }
        test.set("target_id",courseBase.get("id"));
        test.set("target_type","coursetest");
        test.set("paper_make_pattern","3");
        test.set("paper_answer_pattern","2");
        test.set("rand_single_num",SysProperties.dao.getSysPropertiesByName("course_exam_single_choice_num"));
        test.set("rand_multi_num",SysProperties.dao.getSysPropertiesByName("course_exam_multi_choice_num"));
        test.set("rand_choose_num",SysProperties.dao.getSysPropertiesByName("course_exam_determine_choose_num"));
        test.set("rand_fill_num",SysProperties.dao.getSysPropertiesByName("course_exam_fill_blank_num"));
        test.set("rand_single_score",SysProperties.dao.getSysPropertiesByName("course_exam_single_choice_score"));
        test.set("rand_multi_score",SysProperties.dao.getSysPropertiesByName("course_exam_multi_choice_score"));
        test.set("rand_choose_score",SysProperties.dao.getSysPropertiesByName("course_exam_determine_choose_score"));
        test.set("rand_fill_score",SysProperties.dao.getSysPropertiesByName("course_exam_fill_blank_score"));
        test.remove("id").save();
        courseLearningSetting.set("test_id",test.getLong("id"));
        courseLearningSetting.update();
    }

    /**
     * 保存我的考试
     * 练习
     * 试卷信息
     * @param myExamLearningSetting
     * @param test
     */
    public void saveMyTest(MyExamLearningSetting myExamLearningSetting,Test test){
        MyExam myExam = MyExam.dao.findById(myExamLearningSetting.getLong("myexam_id"));
        test.set("title",myExam.get("name"));
        test.set("pass_proportion",SysProperties.dao.getSysPropertiesByName("myexam_test_pass_score"));
        test.set("limited_time",SysProperties.dao.getSysPropertiesByName("myexam_test_limited_time"));
        test.set("score",SysProperties.dao.getSysPropertiesByName("myexam_test_total_score"));
        test.set("item_count",SysProperties.dao.getSysPropertiesByName("myexam_test_item_count"));
        test.set("is_open", "1");
        test.set("missscore","0");
        QuestionCategory questionCategory = QuestionCategory.dao.getQuestionCategory(myExam.getLong("id"),"myexam");
        if(questionCategory==null){
            test.set("rand_categery_id","1,2");
        }else{
            test.set("rand_categery_id",String.valueOf(questionCategory.getLong("id")));
        }
        test.set("target_id",myExam.getLong("id"));
        test.set("target_type","myexamtest");
        test.set("paper_make_pattern","3");
        test.set("paper_answer_pattern","2");
        test.set("rand_single_num",SysProperties.dao.getSysPropertiesByName("myexam_test_single_choice_num"));
        test.set("rand_multi_num",SysProperties.dao.getSysPropertiesByName("myexam_test_multi_choice_num"));
        test.set("rand_choose_num",SysProperties.dao.getSysPropertiesByName("myexam_test_determine_choose_num"));
        test.set("rand_fill_num",SysProperties.dao.getSysPropertiesByName("myexam_test_fill_blank_num"));
        test.set("rand_single_score",SysProperties.dao.getSysPropertiesByName("myexam_test_single_choice_score"));
        test.set("rand_multi_score", SysProperties.dao.getSysPropertiesByName("myexam_test_multi_choice_score"));
        test.set("rand_choose_score",SysProperties.dao.getSysPropertiesByName("myexam_test_determine_choose_score"));
        test.set("rand_fill_score",SysProperties.dao.getSysPropertiesByName("myexam_test_fill_blank_score"));
        test.remove("id").save();
        myExamLearningSetting.set("test_id",test.get("id"));
        myExamLearningSetting.update();
    }
}
